<?php

/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2022-04-21
 * Time: 14:44:49
 * Info:
 */

namespace app\common\model;

use support\lib\Random;

class Admin extends Common
{

    public function authGroup()
    {
        return $this->belongsTo('app\common\model\AuthGroup', 'role_id', 'id');
    }

    /**
     * 获取管理员信息
     * @return array
     */
    public static function getInfo($id)
    {
        $info = (new Admin)->where('id', $id)->find();
        if (empty($info)) {
            return ['code' => 0, 'msg' => '获取信息失败'];
        }
        if (is_object($info)) {
            $info = $info->toArray();
        }

        return ['code' => 200, 'msg' => '获取信息成功', 'data' => $info];
    }

    public static function manageAdd($param)
    {
        //查询用户名是否存在
        $findAdmin = (new Admin)->where(['username' => $param['username']])->find();
        if ( ! empty($findAdmin)) {
            return ['code' => 0, 'msg' => '该管理员账号已存在'];
        }
        $salt              = Random::alnum();
        $param['salt']     = $salt;
        $param['password'] = cmf_password($param['password'], $salt);
        (new Admin)->create($param);

        return ['code' => 200, 'msg' => '添加成功'];
    }

    public static function manageEdit($param)
    {
        //密码不为空的时候修改密码
        if ( ! empty($param['password'])) {
            if ($param['password'] !== $param['password2']) {
                return ['code' => 0, 'msg' => '两次密码输入不一致'];
            }
            $salt              = Random::alnum();
            $param['salt']     = $salt;
            $param['password'] = cmf_password($param['password'], $salt);
        } else {
            unset($param['password']);
        }
        //查询超管账号是否存在
        $findAdmin = (new Admin)->where('id', $param['id'])->find();
        if (empty($findAdmin)) {
            return ['code' => 0, 'msg' => '该会员信息不存在'];
        }
        if (isset($param['status']) && $param['status'] == 0 && $findAdmin['id'] == 1) {
            return ['code' => 0, 'msg' => '禁止停用id=1的超级管理员，如需禁用，请在数据库中操作！'];
        }
        $where = ['id' => $param['id']];
        self::update($param, $where);

        return ['code' => 200, 'msg' => '修改成功'];
    }

    /**
     * 删除操作
     *
     * @param $id
     *
     * @return array
     */
    public static function manageDel($id)
    {
        //判断管理员是否存在
        if ($id == 1) {
            return ['code' => 0, 'msg' => 'id=1的超级管理员不允许删除'];
        }
        $findAdmin = (new Admin)->where('id', $id)->find();
        if (empty($findAdmin)) {
            return ['code' => 0, 'msg' => '该管理员信息不存在'];
        }
        (new Admin)->destroy($id);

        return ['code' => 200, 'msg' => '删除成功'];
    }

    public static function manageStatus($id, $val)
    {
        if ($id == 1 && $val == 0) {
            return ['code' => 0, 'msg' => 'id=1的超级管理员不允许禁用'];
        }
        $findAdmin = (new Admin)->where('id', $id)->find();
        if (empty($findAdmin)) {
            return ['code' => 0, 'msg' => '该管理员信息不存在'];
        }
        (new Admin)->where('id', $id)->data(['status' => $val])->update();

        return ['code' => 200, 'msg' => '更改成功'];
    }

    /**
     * 修改密码
     */
    public static function editPass($param)
    {
        $adminId = cmf_get_admin_id();
        $oldPwd  = $param['oldpwd'];
        $newPwd  = $param['newpwd'];
        //查询旧密码是否正确
        $findAdmin = Admin::where('id', $adminId)->find();
        if (empty($findAdmin)) {
            return ['code' => 0, 'msg' => '管理员不存在'];
        }
        if (cmf_password($oldPwd, $findAdmin['salt']) != $findAdmin['password']) {
            return ['code' => 0, 'msg' => '旧密码不正确'];
        }
        $salt              = Random::alnum();
        $param['salt']     = $salt;
        $param['password'] = cmf_password($newPwd, $salt);

        $where = ['id' => $adminId];
        self::update($param, $where);

        return ['code' => 200, 'msg' => '修改成功'];
    }

}
